package weaver.interfaces.ht.duss.fgdzc;

import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.interfaces.ht.Util.TransMethodUitl;
import weaver.interfaces.ht.Util.WorkflowCreateHandler;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;

import java.util.HashMap;
import java.util.Map;

/**
 * @Title: ecology-9-demo
 * @Company: 泛微软件
 * @author: weaver.Dusongsong
 * @version: 1.0
 * @CreateDate: 2020-06-06
 * @Description: 创建采购申请流程
 * @ModifyLog:
 **/

public class WeaDoFgdzclcAction  extends BaseBean implements Action {
    @Override
    public String execute(RequestInfo requestInfo) {
        BaseBean baseBean = new BaseBean();
        String requestid = requestInfo.getRequestid();
        baseBean.writeLog("requestID" + requestid);
        baseBean.writeLog("in: WeaDoFgdzclcAction");
        String tablename = requestInfo.getRequestManager().getBillTableName();//主表
        String curDate = requestInfo.getRequestManager().getCurrentDate();
        WorkflowCreateHandler handler = new WorkflowCreateHandler();
        RecordSet rs = new RecordSet();
        RecordSet rs1 = new RecordSet();
        RecordSet rs2 = new RecordSet();
        RecordSet rs3 = new RecordSet();
        try {
            rs.executeQuery("select * from " + tablename + " where requestid=" + requestid);
            rs.next();
            String mainid = Util.null2String(rs.getString("id"));//主键
            String xm = Util.null2String(rs.getString("xm"));//申请人-姓名
            // 获取主表id 用于获取mainid插入明细表
            rs1.executeQuery("select count(*) as counts from "+tablename+"_dt1 where mainid='"+mainid+"' and sfth !=''");
            baseBean.writeLog("select count(*) as counts from "+tablename+"_dt1 where mainid='"+mainid+"' and sfth !=''");
            while (rs1.next()) {
                int  counts = Util.getIntValue(rs1.getString("counts"));//是否退回
                if (counts>0) {
                    String creatorId = xm;
                    String workname = TransMethodUitl.transIDtoLastname(xm);
                    String workflowId = "181";
                    String requestName = "非固定资产报废配件入库申请-"+workname+"-"+curDate;
                    String IsNextFlow = "0";
                    Map<String, String> requestMainMap = new HashMap<>();
                    requestMainMap.put("xm", xm);
                    String newRequestid = handler.WorkflowCreateByRequestMap(creatorId, workflowId, requestName, IsNextFlow, requestMainMap);
                    baseBean.writeLog("创建非固定资产待办流程requestID:"+newRequestid);
                    rs2.executeQuery("select * from " + tablename + "_dt1 where mainid=" + mainid+" and sfth !=''");
                    while (rs2.next()){
                    String tp = Util.null2String(rs2.getString("tp"));//图片
                    String sapcode = Util.null2String(rs2.getString("sapcode"));//SAP CODE
                    String po = Util.null2String(rs2.getString("po"));//PO
                    int bfsl = Util.getIntValue(rs2.getString("bfsl"));//报废数量
                    String pm = Util.null2String(rs2.getString("pm"));//品名
                    String dw = Util.null2String(rs2.getString("dw"));//单位
                    int posl = Util.getIntValue(rs2.getString("posl"));//PO数量
                        posl = posl>0 ? posl:0;
                    float jkzje = Util.getFloatValue(rs2.getString("jkzje"));//进口总金额
                    String bz = Util.null2String(rs2.getString("bz"));//币种
                    String gnw = Util.null2String(rs2.getString("gnw"));//国内外
                    String ysjkbgdh = Util.null2String(rs2.getString("ysjkbgdh"));//原始进口报关单号
                    String pm1 = Util.null2String(rs2.getString("pm1"));//品名1
                    String bzhu = Util.null2String(rs2.getString("bzhu"));//备注
                    float dj = Util.getFloatValue(rs2.getString("dj"));//单价
                        dj = dj>0 ? dj:0;
                    rs3.executeQuery("select id from " + tablename + " where requestid=" + newRequestid);
                    rs3.next();
                    int newmainid = Util.getIntValue(rs3.getString("id"));
                    String INSERT_SQL = "insert into " + tablename + "_dt1 (" +
                            "mainid,tp,sapcode,po,bfsl,pm,dw,posl,jkzje,bz," +
                            "gnw,ysjkbgdh,pm1,bzhu,dj)" +
                            " values('" + newmainid + "','" + tp + "','" + sapcode + "','" + po + "','" + bfsl + "','" + pm + "','" + dw + "','" + posl + "','" + jkzje + "','" + bz + "'," +
                            "'" + gnw + "','" + ysjkbgdh + "','" + pm1 + "','" + bzhu + "','" + dj + "')";
                    rs3.executeUpdate(INSERT_SQL);
                    }
                }
            }
            return Action.SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            baseBean.writeLog("异常参考:" + e);
            requestInfo.getRequestManager().setMessageid("111111");
            requestInfo.getRequestManager().setMessagecontent(e.getMessage());
            return Action.FAILURE_AND_CONTINUE;
        }
    }
}
